package in.iqing.view.activity;

import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.ViewGroup;

import com.squareup.picasso.Callback;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;

import java.io.BufferedOutputStream;
import java.io.File;
import java.util.Random;

import butterknife.Bind;
import butterknife.OnClick;
import in.iqing.app.R;
import in.iqing.base.BaseActivity;
import in.iqing.control.helper.ImageHelper;
import in.iqing.control.util.ToastUtil;
import in.iqing.model.bean.Page;
import in.iqing.model.sp.SettingSP;
import uk.co.senab.photoview.PhotoView;

/**
 * Created by neavo on 2014/6/29.
 */

public class IllustrationActivity extends BaseActivity {

    @Bind(R.id.illustration_photoview)
    public PhotoView illustrationPhotoView;

    @Bind(R.id.top_menu)
    public ViewGroup topMenu;

    @Bind(R.id.bottom_menu)
    public ViewGroup bottomMenu;

    @Bind(R.id.content_layout)
    public ViewGroup contentLayout;

    private boolean showingMenu;

    private Page page;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_illustration);
    }

    @Override
    protected void onInitialize(Bundle savedInstanceState) {
        super.onInitialize(savedInstanceState);

        initVariables();
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);

        setBackground();
        setIllustrationPhotoView();
    }

    private void setBackground() {
        contentLayout.setBackgroundResource(SettingSP.getSingleton(getContext()).getNightMode()
                ? R.drawable.content_night_bg
                : R.drawable.content_day_bg);
    }

    @OnClick(R.id.back)
    public void onBackClick(View v){
        finish();
    }

    @OnClick(R.id.save_image)
    public void onSaveImageClick(View v) {
        Drawable drawable = illustrationPhotoView.getDrawable();

        if (drawable instanceof BitmapDrawable) {
            Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();

            BufferedOutputStream bufferedOS = null;
            File target = new File(Environment.getExternalStorageDirectory() + "/" + new Random().nextInt(100000000) + ".jpg");

            try {
                bufferedOS = new BufferedOutputStream(FileUtils.openOutputStream(target));
                bitmap.compress(Bitmap.CompressFormat.JPEG, 75, bufferedOS);

                ToastUtil.show(getActivity(), getString(R.string.activity_illustration_save_success, target));
            } catch (Exception e) {
                e.printStackTrace();
                ToastUtil.show(getActivity(), e.getMessage());
            } finally {
                IOUtils.closeQuietly(bufferedOS);
            }
        }
    }

    @OnClick(R.id.rotation_image)
    public void onRotationImageClick(View v) {
        Drawable drawable = illustrationPhotoView.getDrawable();

        if (drawable instanceof BitmapDrawable) {
            Matrix matrix = new Matrix();
            Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();

            matrix.setRotate(90);
            illustrationPhotoView.setImageBitmap(
                    Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true)
            );
        }
    }

    private void initVariables() {
        this.page = (Page) getIntent().getExtras().getSerializable("page");
    }

    private void setIllustrationPhotoView() {
        showLoading();

        ImageHelper.getSingleton(getContext()).load(
                page.getLines().get(0),
                illustrationPhotoView,
                new ExtCallback()
        );
    }

    private class ExtCallback implements Callback {

        @Override
        public void onError() {
            ToastUtil.show(getContext(), getString(R.string.activity_content_image_error));
        }

        @Override
        public void onSuccess() {
            showContent();
        }
    }
}
